//
//  verifyViewController.m
//  TinyFortune
//
//  Created by lc on 16/5/25.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "verifyViewController.h"


@interface verifyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *headerLabel;
- (IBAction)vertifyAction:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UILabel *tryAgainLabel;

@end

@implementation verifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //增加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    _tryAgainLabel.userInteractionEnabled = YES ;
    [_tryAgainLabel addGestureRecognizer:tap];
    _headerLabel.text = [NSString stringWithFormat:@"我们已发送验证码到您手机呢   %@",self.telephoneNumber];
    //导航栏
    self.navigationController.navigationBar.translucent = NO ;
    [self.navigationController setNavigationBarHidden:NO];
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    [self.navigationItem setLeftBarButtonItem:left];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5]];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.title = @"验证手机";
    [self.codeText setKeyboardType:UIKeyboardTypePhonePad];
    
}
- (void)tap:(UITapGestureRecognizer *)sender{
    //当网络不好，或者服务器繁忙，未接收到验证码时，给服务器发送请求，再发送一次验证码
    [AVOSCloud requestSmsCodeWithPhoneNumber:_codeText.text appName:@"小幸运" operation:@"注册账户" timeToLive:10 callback:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            
        }else{
            [KVNProgress showErrorWithStatus:error.localizedDescription];
            
            
        }
        
    }];
}
- (void)cancel{
    [self.navigationController popToRootViewControllerAnimated:YES];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)vertifyAction:(id)sender {
    //短信验证提交结果
    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:_telephoneNumber smsCode:_codeText.text block:^(AVUser *user, NSError *error) {
       //如果注册成功了以后就回到根视图控制器
        if (error == NULL) {
            [KVNProgress showWithStatus:@"注册成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [KVNProgress showErrorWithStatus:error.localizedDescription];
        }
    }];
}
@end
